<?php
 
use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;

class LessonsController extends ControllerBase
{

    /**
     * Index action
     */
    public function indexAction()
    {
        $this->persistent->parameters = null;
    }

    /**
     * Searches for lessons
     */
    public function searchAction()
    {

        $numberPage = 1;
        if ($this->request->isPost()) {
            $query = Criteria::fromInput($this->di, "Lessons", $_POST);
            $this->persistent->parameters = $query->getParams();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
        }

        $parameters = $this->persistent->parameters;
        if (!is_array($parameters)) {
            $parameters = array();
        }
        $parameters["order"] = "id";

        $lessons = Lessons::find($parameters);
        if (count($lessons) == 0) {
            $this->flash->notice("The search did not find any lessons");

            return $this->dispatcher->forward(array(
                "controller" => "lessons",
                "action" => "index"
            ));
        }

        $paginator = new Paginator(array(
            "data" => $lessons,
            "limit"=> 10,
            "page" => $numberPage
        ));

        $this->view->page = $paginator->getPaginate();
    }

    /**
     * Displayes the creation form
     */
    public function newAction()
    {

    }

    /**
     * Edits a lesson
     *
     * @param string $id
     */
    public function editAction($id)
    {

        if (!$this->request->isPost()) {

            $lesson = Lessons::findFirstByid($id);
            if (!$lesson) {
                $this->flash->error("lesson was not found");

                return $this->dispatcher->forward(array(
                    "controller" => "lessons",
                    "action" => "index"
                ));
            }

            $this->view->id = $lesson->id;

            $this->tag->setDefault("id", $lesson->id);
            $this->tag->setDefault("name", $lesson->name);
            $this->tag->setDefault("type", $lesson->type);
            $this->tag->setDefault("img", $lesson->img);
            $this->tag->setDefault("description", $lesson->description);
            $this->tag->setDefault("level", $lesson->level);
            $this->tag->setDefault("note", $lesson->note);
            
        }
    }

    /**
     * Creates a new lesson
     */
    public function createAction()
    {

        if (!$this->request->isPost()) {
            return $this->dispatcher->forward(array(
                "controller" => "lessons",
                "action" => "index"
            ));
        }

        $lesson = new Lessons();

        $lesson->name = $this->request->getPost("name");
        $lesson->type = $this->request->getPost("type");
        $lesson->img = $this->request->getPost("img");
        $lesson->description = $this->request->getPost("description");
        $lesson->level = $this->request->getPost("level");
        $lesson->note = $this->request->getPost("note");
        

        if (!$lesson->save()) {
            foreach ($lesson->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                "controller" => "lessons",
                "action" => "new"
            ));
        }

        $this->flash->success("lesson was created successfully");

        return $this->dispatcher->forward(array(
            "controller" => "lessons",
            "action" => "index"
        ));

    }

    /**
     * Saves a lesson edited
     *
     */
    public function saveAction()
    {

        if (!$this->request->isPost()) {
            return $this->dispatcher->forward(array(
                "controller" => "lessons",
                "action" => "index"
            ));
        }

        $id = $this->request->getPost("id");

        $lesson = Lessons::findFirstByid($id);
        if (!$lesson) {
            $this->flash->error("lesson does not exist " . $id);

            return $this->dispatcher->forward(array(
                "controller" => "lessons",
                "action" => "index"
            ));
        }

        $lesson->name = $this->request->getPost("name");
        $lesson->type = $this->request->getPost("type");
        $lesson->img = $this->request->getPost("img");
        $lesson->description = $this->request->getPost("description");
        $lesson->level = $this->request->getPost("level");
        $lesson->note = $this->request->getPost("note");
        

        if (!$lesson->save()) {

            foreach ($lesson->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                "controller" => "lessons",
                "action" => "edit",
                "params" => array($lesson->id)
            ));
        }

        $this->flash->success("lesson was updated successfully");

        return $this->dispatcher->forward(array(
            "controller" => "lessons",
            "action" => "index"
        ));

    }

    /**
     * Deletes a lesson
     *
     * @param string $id
     */
    public function deleteAction($id)
    {

        $lesson = Lessons::findFirstByid($id);
        if (!$lesson) {
            $this->flash->error("lesson was not found");

            return $this->dispatcher->forward(array(
                "controller" => "lessons",
                "action" => "index"
            ));
        }

        if (!$lesson->delete()) {

            foreach ($lesson->getMessages() as $message) {
                $this->flash->error($message);
            }

            return $this->dispatcher->forward(array(
                "controller" => "lessons",
                "action" => "search"
            ));
        }

        $this->flash->success("lesson was deleted successfully");

        return $this->dispatcher->forward(array(
            "controller" => "lessons",
            "action" => "index"
        ));
    }

}
